package edu.must.uh.util;

import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SingleResultSetHandlerFactory {
    
    public static <T> ResultSetHandler<T> newObjectHandler() {
        return new ResultSetHandler<T>() {
            @Override
            public T handle(ResultSet resultSet) throws SQLException {
                if (resultSet.next()) {
                    return (T)resultSet.getObject(1);
                }
                return null;
            }
        };
    }

    public static <T> ResultSetHandler<List<T>> newListObjectHandler() {
        return new ResultSetHandler<List<T>>() {
            @Override
            public List<T> handle(ResultSet resultSet) throws SQLException {
                List<T> ret = new ArrayList<T>();
                while (resultSet.next()) {
                    ret.add((T)resultSet.getObject(1));
                }
                return ret;
            }
        };
    }

}
